﻿using System.Diagnostics;
using LFSRecord2.Model.Undo;

namespace LFSRecord2.Model.UserActions
{
    public class UserActionDeleteKeyFrame : UserActionBase
    {
        // Action description, so we can replay this action (and reverse it)
        public int LayerIndex { get; set; }
        public int PropertyIndex { get; set; }

        /// <summary>
        /// The key frame index after insertion. With that we know how to reverse and replay it.
        /// </summary>
        public KeyFrameDataBase KeyFrame;

        public UserActionDeleteKeyFrame()
        {
            Action = UserActionTypes.DeleteKeyFrame;
        }

        public override void DoUndo()
        {
            Debug.WriteLine("Undoing a DeleteKeyFrame action");
            LfsRecordController.project.DeselectAll();
            LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].ReinstateKeyFrame(KeyFrame);
            LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.All;
        }

        public override void DoRedo()
        {
            Debug.WriteLine("Redoing a DeleteKeyFrame action");
            for (int c = 0; c < LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.Count; c++)
            {
                CustomKeyFrame kf = LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[c];
                if (kf.KeyFrameIndex == KeyFrame.KeyFrameIndex)
                {
                    LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames[c].Dispose();
                    LfsRecordController.project.Layers[LayerIndex].Properties[PropertyIndex].KeyFrames.RemoveAt(c);
                    LfsRecordController.project.Layers[LayerIndex].UpdatedCurrentValues = CompLayerPropertyUpdateTypes.All;
                    break;
                }
            }
        }
    }
}
